<template>
	<view class="container">
		<view class="user-section">
			<image class="bg" src="/static/images/user-bg.jpg"></image>
			<view class="box-cont">
				<view class="user-info-box">
					<view class="portrait-box" @click="navTo('/pages/user/userinfo')">
						<image class="portrait" :src="userInfo.headimg || '/static/logo.png'"></image>
					</view>
					<view class="info-box">
						<view class="username">{{userInfo.mobile || '请登录'}} <text v-if="userInfo.isshop == 1">(店铺)</text></view>
						<view class="tip">用户等级:{{ userInfo.viplevelname || '无' }}</view>
					</view>
				</view>
			</view>
		</view>
		
		<view
			class="cover-container"
			:style="[
				{
					transform: coverTransform,
					transition: coverTransition
				}
			]"
			@touchstart="coverTouchstart"
			@touchmove="coverTouchmove"
			@touchend="coverTouchend"
		>
			<image class="arc" src="/static/images/arc.png"></image>
			<view class="vip-card-box">
				<view class="b-btn" @click="navTo('/pages/user/verified')" v-if="userInfo.identitystatus == 0 || userInfo.identitystatus == undefined">未实名</view>
				<view class="b-btn" @click="navTo('/pages/user/verified_info')" v-else>已实名</view>
				<view class="tit">
					<text class="yticon icon-iLinkapp-"></text>
					实名认证
				</view>
			</view>
			
			<view class="tj-sction">
				<view class="tj-item" @click="$api.toUrl(`/pages/money/money`)">
					<text class="num">{{ userInfo.money || '0' }}</text>
					<text>可用余额</text>
				</view>
				<view class="tj-item" @click="$api.toUrl('/pages/money/cash')">
					<text class="num">{{ userInfo.cash || '0' }}</text>
					<text>可用购物积分</text>
				</view>
			</view>
			
			<!-- 订单 -->
			<view class="order-section">
				<view class="order-tit">
					<view class="tit">订单中心</view>
				</view>
				<view class="order-cont">
					<view class="cate-item" @click="$api.toUrl('/pages/order/order_menu0')">
						<image src="/static/images/dd1.png"></image>
						<text>自营区订单</text>
					</view>
					<view class="cate-item" @click="$api.toUrl('/pages/order/order_menu1')">
						<image src="/static/images/dd2.png"></image>
						<text>消费区订单</text>
					</view>
					<view class="cate-item" @click="$helper.toast('暂未开通，敬请期待');">
						<image src="/static/images/dd3.png"></image>
						<text>店铺区订单</text>
					</view>
				</view>
			</view>
			
			<!-- 点击跳转二维码页面开始 -->
			<!-- <navigator url="/pages/share/QRcode">
				<view class="shareImage"><image src="/static/images/share.png" mode="widthFix"></image></view>
			</navigator> -->
			<!-- 点击跳转二维码页面结束 -->
			
			<view class="my-service">
				<view class="title">
					<text>常用功能</text>
				</view>
				<view class="service-list">
					<view class="list" @click="$api.toUrl('/pages/user/recharge')">
						<view class="thumb">
							<text class="yticon icon-iconfontweixin" style="color: #54b4ef;"></text>
						</view>
						<view class="name">
							<text>线下充值</text>
						</view>
					</view>
					<view class="list" @click="$api.toUrl('/pages/address/address')">
						<view class="thumb">
							<text class="yticon icon-dizhi" style="color: #54b4ef;"></text>
						</view>
						<view class="name">
							<text>地址管理</text>
						</view>
					</view>
					<view class="list" @click="$api.toUrl('/pages/bank/bank')">
						<view class="thumb">
							<text class="yticon icon-bianji" style="color: #54b4ef;"></text>
						</view>
						<view class="name">
							<text>收款账户</text>
						</view>
					</view>
					<view class="list" @click="$api.toUrl('/pages/team/invite')">
						<view class="thumb">
							<text class="yticon icon-tuandui" style="color: #54b4ef;"></text>
						</view>
						<view class="name">
							<text>我的好友</text>
						</view>
					</view>
				</view>
			</view>
			
			
			<view class="list-cell b-b margin-top-sm" @click="navTo('/pages/notice/notice')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">通知公告</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b" @click="navTo('/pages/user/about')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">关于我们</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b " @click="navTo('/pages/user/after-sale')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">联系客服</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b " @click="navTo('/pages/set/set')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">设置</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b "  @click="toLogout" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit" @click="toLogout">退出登录</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			
		</view>
	</view>
</template>
<script>
import { mapMutations, mapState } from 'vuex';
import listCell from '@/components/mix-list-cell';

let startY = 0,
	moveY = 0,
	pageAtTop = true;

export default {
	components: {
		listCell
	},
	data() {
		return {
			token: '',
			userInfo: {},
			teaminfo: {},
			
			coverTransform: 'translateY(0px)',
			coverTransition: '0s',
			moving: false
		};
	},
	onLoad() {
		
	},
	onShow() {
		this.token = this.$api.get('token');
		if (!this.token) {
			uni.navigateTo({
				url: '/pages/public/login'
			});
			return;
		}
		//获取用户信息
		this.getmedate();
		//获取团队信息
		this.getteamdate();
	},
	// #ifndef MP
	onNavigationBarButtonTap(e) {
		const index = e.index;
		if (index === 0) {
			this.navTo('/pages/share/QRcode');
		} else if (index === 1) {
			// #ifdef APP-PLUS
			const pages = getCurrentPages();
			const page = pages[pages.length - 1];
			const currentWebview = page.$getAppWebview();
			currentWebview.hideTitleNViewButtonRedDot({
				index
			});
			// #endif
			uni.navigateTo({
				url: '/pages/notice/notice'
			});
		}
	},
	// #endif
	computed: {
		//对全局变量进行监控
		...mapState(['hasLogin'])
	},
	methods: {
		//对全局方法进行监控
		...mapMutations(['login']),
		...mapMutations(['logout']),

		/**
		 * 统一跳转接口,拦截未登录路由
		 * navigator标签现在默认没有转场动画，所以用view
		 */
		navTo(url) {
			if (!this.token) {
				url = '/pages/public/login';
			}
			uni.navigateTo({
				url
			});
		},

		/**
		 *  会员卡下拉和回弹
		 *  1.关闭bounce避免ios端下拉冲突
		 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
		 *    transition设置0.1秒延迟，让css来过渡这段空窗期
		 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
		 */
		coverTouchstart(e) {
			if (pageAtTop === false) {
				return;
			}
			this.coverTransition = 'transform .1s linear';
			startY = e.touches[0].clientY;
		},
		coverTouchmove(e) {
			moveY = e.touches[0].clientY;
			let moveDistance = moveY - startY;
			if (moveDistance < 0) {
				this.moving = false;
				return;
			}
			this.moving = true;
			if (moveDistance >= 80 && moveDistance < 100) {
				moveDistance = 80;
			}

			if (moveDistance > 0 && moveDistance <= 80) {
				this.coverTransform = `translateY(${moveDistance}px)`;
			}
		},
		coverTouchend() {
			if (this.moving === false) {
				return;
			}
			this.moving = false;
			this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
			this.coverTransform = 'translateY(0px)';
		},
		//获取用户信息
		async getmedate() {
			const param_userinfo = {
				token: this.token
			};
			await this.$http.request('User', 'userinfo', 'GET', param_userinfo, res => {
				if (res.status == 1) {
					if (res.data.error == 0) {
						this.userInfo = res.data.data;
						this.login(JSON.stringify(res.data.data));
					}
				}
			});
		},
		//获取团队信息
		async getteamdate() {
			const param_userinfo = {
				token: this.token
			};
			await this.$http.request('User', 'teaminfo', 'GET', param_userinfo, res => {
				if (res.status == 1) {
					if (res.data.error == 0) {
						this.teaminfo = res.data.data;
						this.login(JSON.stringify(res.data.data));
					}
				}
			});
		},
		
		//退出登录
		toLogout() {
			uni.showModal({
				content: '确定要退出登录么',
				success: e => {
					if (e.confirm) {
						this.logout();
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/public/login'
							});
						}, 200);
					}
				}
			});
		},
		
	}
};
</script>
<style lang="scss">
%flex-center {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}
%section {
	display: flex;
	justify-content: space-around;
	align-content: center;
	background: #fff;
	border-radius: 10upx;
}

.user-section {
	height: 400upx;
	padding: 50upx 30upx 0;
	position: relative;
	.bg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		filter: blur(1px);
		opacity: 0.7;
	}
}
.box-cont {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.user-info-box {
	height: 180upx;
	display: flex;
	align-items: center;
	position: relative;
	z-index: 1;
	
	.portrait {
		width: 130upx;
		height: 130upx;
		border: 5upx solid #fff;
		border-radius: 50%;
	}

	.username {
		font-size: $font-sm + 14upx;
		color: #FFFFFF;
		margin-left: 20upx;
	}

	.tip {
		font-size: $font-sm + 2upx;
		color: #ffffff;
		margin-left: 20upx;
		margin-top: 10upx;
	}
	
	.money {
		font-size: $font-sm + 10upx;
		color: #ffffff;
		margin-left: 20upx;
		margin-top: 20upx;
	}
}

.cover-container {
	background: $page-color-base;
	margin-top: -150upx;
	padding: 0 30upx;
	position: relative;
	background: #f5f5f5;
	padding-bottom: 20upx;
	.arc {
		position: absolute;
		left: 0;
		top: -34upx;
		width: 100%;
		height: 36upx;
	}
}

.vip-card-box {
	display: flex;
	flex-direction: column;
	color: #000000;
	background-color: #fff;
	height: 70upx;
	border-radius: 16upx 16upx 0 0;
	overflow: hidden;
	position: relative;
	padding: 20upx 24upx;
	
	.b-btn {
		position: absolute;
		right: 20upx;
		top: 22upx;
		width: 132upx;
		height: 40upx;
		text-align: center;
		line-height: 40upx;
		font-size: $font-base + 2upx;
		color: #36343c;
		z-index: 1;
	}
	.tit {
		font-size: $font-base + 2upx;
		color: #000000;
		.yticon {
			color: #000000;
			margin-right: 16upx;
		}
	}
	.e-b {
		font-size: $font-sm;
		color: #d8cba9;
		margin-top: 10upx;
	}
}

.tj-sction {
	@extend %section;
	margin-top: 10upx;
	background-color: #ffffff;
	
	.tj-item {
		@extend %flex-center;
		flex-direction: column;
		height: 120upx;
		font-size: $font-sm;
		color: #808080;
	}
	.num {
		font-size: 36upx;
		color: #808080;
		margin-bottom: 8upx;
	}
}

.cmoney-sction {
	margin-top: 20upx;
	@extend %section;
	.tj-item {
		@extend %flex-center;
		flex-direction: column;
		height: 100upx;
		font-size: $font-sm;
		color: #75787d;
	}
	.num {
		font-size: $font-lg;
		color: $font-color-red;
		margin-bottom: 8upx;
	}
}

.shareImage {
	margin-top: 20upx;
	text-align: center;
	position: relative;

	image {
		height: 88upx;
		width: 100%;
	}
}

.user-sction {
	@extend %section;
	margin-top: 10upx;
	background-color: #EDE1D9;
	
	.tj-item {
		@extend %flex-center;
		flex-direction: column;
		height: 140upx;
		font-size: $font-sm;
		color: #75787d;
	}
	.num {
		font-size: $font-lg;
		color: $font-color-spec;
		margin-bottom: 8upx;
	}
}

.order-section{
	background:#ffffff;
	border-radius: 20upx;
	margin-top: 20upx;
	padding: 0 20upx;
	
	.order-tit{
		height: 80upx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		flex-wrap: nowrap;
		
		.tit{
			font-size: 34upx;
			font-weight: bold;
		}
	}
	.order-cont {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		padding: 0upx 20upx;
		background: #fff;
		
		.cate-item {
			display: flex;
			flex-direction: column;
			margin: 20upx 0upx 20upx 0upx;
			align-items: center;
			font-size: 26upx;
		}
		
		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 70upx;
			height: 70upx;
			margin-bottom: 14upx;
			opacity: 0.7;
		}
	}
}


.my-service {
	width: 100%;
	background-color: #FFFFFF;
	border-radius: 20rpx;
	margin: 20rpx auto;
	margin-top: 10rpx;

	.title {
		display: flex;
		align-items: center;
		padding: 0 4%;
		height: 80rpx;

		text {
			font-size: 28rpx;
			font-weight: bold;
			color: #000000;
		}
	}

	.service-list {
		display: flex;
		flex-wrap: wrap;
		padding: 0 4%;

		.list {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 25%;
			height: 180upx;

			.thumb {
				width: 80upx;
				height: 80upx;
				
				.yticon {
					font-size: 80upx;
					color: #fa436a;
				}

			}

			.name {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 80upx;

				text {
					color: #212121;
					margin-top: 10upx;
					font-size: 26upx;
				}
			}
		}
	}
}


.list-cell{
	display:flex;
	align-items:baseline;
	padding: 20upx $page-row-spacing;
	line-height:60upx;
	position:relative;
	background: #fff;
	justify-content: center;
	&.log-out-btn{
		margin-top: 40upx;
		.cell-tit{
			color: $uni-color-primary;
			text-align: center;
			margin-right: 0;
		}
	}
	&.cell-hover{
		background:#fafafa;
	}
	&.b-b:after{
		left: 30upx;
	}
	&.m-t{
		margin-top: 16upx; 
	}
	.cell-more{
		align-self: baseline;
		font-size:$font-lg;
		color:$font-color-light;
		margin-left:10upx;
	}
	.cell-tit{
		flex: 1;
		font-size: $font-base + 2upx;
		color: $font-color-dark;
		margin-right:10upx;
	}
	.cell-tip{
		font-size: $font-base;
		color: $font-color-light;
	}
	switch{
		transform: translateX(16upx) scale(.84);
	}
}
	

</style>
